package com.xqboss.apps.service.user;


import com.xqboss.apps.vo.user.UserIdentityPermWrenchVo;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.user.UserIdentityPermWrench;
import com.xqboss.apps.mapper.user.UserIdentityPermWrenchMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;


/**
 * 用户身份-权限-邀返扳手(UserIdentityPermWrench)表服务实现类
 *
 * @author makejava
 * @since 2023-12-07 13:59:34
 */
@Service
public class UserIdentityPermWrenchService extends ServiceImpl<UserIdentityPermWrenchMapper, UserIdentityPermWrench> {

    @Autowired
    private UserIdentityService userIdentityService;


    /**
     * 获取有效的邀返扳手配置
     * @param userId
     * @param nftId
     */
    public UserIdentityPermWrench getEffective(Long userId, Long nftId) {
        return this.lambdaQuery()
                .eq(UserIdentityPermWrench::getIdentityId, userIdentityService.getByUserId(userId).getId())
                .eq(UserIdentityPermWrench::getUseNftId, nftId)
                .eq(UserIdentityPermWrench::getIsOpen, 1)
                .last("limit 1")
                .one();
    }

    /**
     * 邀请返材料配置：
     *
     * - 返材料开关：默认选中【关闭】，关闭后该身份将不发放材料，关闭后触发条件、一级邀请、二级邀请字段不显示
     * - 触发条件：点击【添加按钮】，弹出【藏品列表】弹窗，选中提交后返显【藏品名称 藏品ID】
     * - 一级邀请：正数，保留两位数量，一级邀请用户使用1张对应藏品，邀请人将获得扳手的数量；
     * A邀请B，B邀请C，A为邀请人、B为一级邀请、C为二级邀请，B使用2张对应藏品，A获得 ＝ 2 × 一级邀请返扳手数量
     * 二级邀请：正数，保留两位数量，二级邀请用户使用1张对应藏品，邀请人将获得扳手的数量；
     *
     * @param vo 请求对象
     */
    @Transactional(rollbackFor=Exception.class)
    public void invitationToReturnMaterials(UserIdentityPermWrenchVo vo) {
        UserIdentityPermWrench permWrench = new UserIdentityPermWrench();
        BeanUtils.copyBeanProp(permWrench,vo);
        if (vo.getIsOpen() == 1) {
            if (vo.getFromLevel2ReturnAmount() == null || vo.getFromLevel1ReturnAmount() == null) {
                throw new ServiceException("一级邀请获得扳手的数量和二级邀请获得扳手的数量不能为空！");
            }
            if (vo.getUseNftId() == null) {
                throw new ServiceException("藏品不能为空！");
            }
            if(vo.getFromLevel1ReturnAmount().compareTo(BigDecimal.ZERO)<0){
                throw new ServiceException("一级邀请获得扳手的数量必须为正数！");
            }
            if(vo.getFromLevel2ReturnAmount().compareTo(BigDecimal.ZERO)<0){
                throw new ServiceException("二级邀请获得扳手的数量必须为正数！");
            }
            //如果不需要四舍五入，可以使用RoundingMode.DOWN；RoundingMode.HALF_DOWN表示 五舍六入，负数先取绝对值再五舍六入再负数，RoundingMode.HALF_UP:表示四舍五入，负数先取绝对值再五舍六入再负数。
            vo.setFromLevel1ReturnAmount(vo.getFromLevel1ReturnAmount().setScale(2, RoundingMode.HALF_UP));
            vo.setFromLevel1ReturnAmount(vo.getFromLevel2ReturnAmount().setScale(2, RoundingMode.HALF_UP));
        }

        this.saveOrUpdate(permWrench);
    }

    /**
     * 获取邀返扳手
     *
     * @param identityId 身份id
     */
    public UserIdentityPermWrench invitationToReturnMaterialsInfo(Long identityId) {
        UserIdentityPermWrench one = this.lambdaQuery().eq(UserIdentityPermWrench::getIdentityId, identityId).one();
        return one==null?new UserIdentityPermWrench():one;
    }
}
